package servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.impl.CaseHistoryDaoImpl;
import entity.CaseHistory;
import util.DateUtil;
import util.NumberCreateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "病历信息操作", value = "/operateCaseHistoryServlet")
public class OperateCaseHistoryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        ObjectMapper mapper = new ObjectMapper();
        int i = 0;
        String plan = "";
        String json = "";
        String flag = request.getParameter("purpose");//*******
        String ch_number = NumberCreateUtil.chNumberRan();
        String ch_date = DateUtil.getStringDateShort();
        String ch_updateDate = DateUtil.getStringDateShort();
        String ch_doctorid = request.getParameter("Doctor_id");
        String ch_patientid = request.getParameter("Patient_id");
        String ch_symptom = request.getParameter("case_history_this_symptom");
        String ch_checkitem = request.getParameter("case_history_check_item");
        String ch_result = request.getParameter("case_history_checkResult");
        String ch_clinicaldiagnosis = request.getParameter("case_history_clinical_diagnosis");
        plan = request.getParameter("case_history_Shuye");
        plan = plan + request.getParameter("case_history_medicine");
        plan = plan + request.getParameter("case_history_surgery");
        String ch_plan = plan;
        String ch_druglist = request.getParameter("case_history_medicine_name");
        String ch_advice = request.getParameter("case_history_advice");
        String ch_Degree_symptoms = request.getParameter("case_history_hospitalized");
        CaseHistory caseHistory = new CaseHistory(ch_number, ch_date, ch_updateDate, ch_doctorid,
                ch_patientid, ch_symptom, ch_checkitem, ch_result, ch_clinicaldiagnosis, ch_plan, ch_druglist, ch_advice, ch_Degree_symptoms);
        if (flag.equals("0")) {
            i = caseHistoryDao.add(caseHistory);
            System.out.println(caseHistory.toString());
        } else if (flag.equals("1")) {
            caseHistory.setCh_number(request.getParameter("CsNum"));
            System.out.println(caseHistory.toString());
            i = caseHistoryDao.update(caseHistory);
        }
        if (i == 1) {
            json = "{\"flag\":\"成功提交信息\"}";
        } else {
            json = "{\"flag\":\"提交信息失败\"}";
        }
        response.setContentType("text/html;charset=UTF-8");
        mapper.writeValue(response.getWriter(), json);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}